﻿#region <!-- Using Directives -->

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

#endregion

namespace WordSharpDev.Extensions
{
	internal static class SqlExtensions
	{
		#region <!-- CreateCommand -->

		public static SqlCommand CreateCommand(this SqlConnection p_This, String p_Text)
		{
			if (p_This == null) return null;

			var l_Command = p_This.CreateCommand();

			l_Command.CommandType = CommandType.Text;
			l_Command.CommandText = p_Text;

			return l_Command;
		}

		#endregion

		#region <!-- AddParamBinary -->

		public static void AddParamBinary(this SqlCommand p_This, String p_Name, Byte[] p_Value)
		{
			if (p_This == null) return;

			var l_Param = p_This.CreateParameter();

			l_Param.Direction = ParameterDirection.Input;

			l_Param.SqlDbType = SqlDbType.VarBinary;
			l_Param.Size = Int32.MaxValue;

			l_Param.ParameterName = p_Name;
			l_Param.Value = p_Value;

			p_This.Parameters.Add(l_Param);
		}

		#endregion

		#region <!-- AddParamString -->

		public static void AddParamString(this SqlCommand p_This, Int32 p_Size, String p_Name, String p_Value)
		{
			if (p_This == null) return;

			var l_Param = p_This.CreateParameter();

			l_Param.Direction = ParameterDirection.Input;

			l_Param.DbType = DbType.String;
			l_Param.Size = p_Size;

			l_Param.ParameterName = p_Name;
			l_Param.Value = p_Value;

			p_This.Parameters.Add(l_Param);
		}

		#endregion

		#region <!-- AddParamXml -->

		public static void AddParamXml(this SqlCommand p_This, String p_Name, String p_Value)
		{
			if (p_This == null) return;

			var l_Param = p_This.CreateParameter();

			l_Param.Direction = ParameterDirection.Input;

			l_Param.DbType = DbType.Xml;

			l_Param.ParameterName = p_Name;
			l_Param.Value = p_Value;

			p_This.Parameters.Add(l_Param);
		}

		#endregion
	}
}